import { EOperator, IConditions } from '@/components/business/Conditions/type';
import { IAssignments } from '@/components/business/Nodes/Rule/types';

/**
 * 校验条件
 * @param conditions
 * @param label
 * ret string[]
 */
export const validateConditions = (conditions: IConditions, label: string) => {
  let errorMsg: string[] = [];
  if (!conditions) {
    errorMsg.push('条件不可为空');
    return errorMsg;
  }
  const conds = conditions.conditions;
  for (let index = 0; index < conds.length; index++) {
    const element = conds[index];
    if (element) {
      if (!element.left) {
        const desc = `${label}：第${index + 1}个条件的左操作数不可为空`;
        errorMsg.push(desc);
      }
      if (!element.operator) {
        const desc = `${label}：第${index + 1}个条件的运算符不可为空`;
        errorMsg.push(desc);
      }
      if (!element.rightType && element.operator !== EOperator.IsEmpty && element.operator !== EOperator.IsNotEmpty) {
        const desc = `${label}：第${index + 1}个条件的值来源不可为空`;
        errorMsg.push(desc);
      }
      if (
        !element.right &&
        element.right !== 0 && element.right !== false &&
        element.operator !== EOperator.IsEmpty && element.operator !== EOperator.IsNotEmpty
      ) {
        const desc = `${label}：第${index + 1}个条件的值不可为空`;
        errorMsg.push(desc);
      }
    }
  }
  return errorMsg;
};

// 校验表达式
export const validateAssignments = (assignments: IAssignments[], label: string) => {
  let errorMsg: string[] = [];
  if (!assignments || !assignments.length) {
    return errorMsg;
  }
  for (let index = 0; index < assignments.length; index++) {
    const element = assignments[index];
    if (element) {
      if (!element.left) {
        const desc = `${label}：第${index + 1}个赋值的变量不可为空`;
        errorMsg.push(desc);
      }
      if (!element.rightType) {
        const desc = `${label}：第${index + 1}个赋值的变量类型不可为空`;
        errorMsg.push(desc);
      }
      if (!element.rightFieldType) {
        const desc = `${label}：第${index + 1}个赋值的值类型不可为空`;
        errorMsg.push(desc);
      }
      if (
        !element.right &&
        element.right !== 0 && element.right !== false
      ) {
        const desc = `${label}：第${index + 1}个赋值的值不可为空`;
        errorMsg.push(desc);
      }
    }
  }
  return errorMsg;
};
